/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jul 3, 2012  8:24:05 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.construction.contract
 * File Name : ContractContractorHighRiseProcessing.java
 * TODO
 */
package apps.business.process.construction.contract;

import java.util.List;

import jpa.entitys.promis.construction.SyncPrBudget;
import jpa.entitys.promis.construction.VwSyncContract;
import jpa.entitys.synchro.staging.MmPocontractorOutb;
import jpa.entitys.synchro.staging.MmPurchaseRequest;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import sap.business.mm.outbound.SyncPrItemService;
import sap.business.mm.outbound.VwSyncContractService;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.common.PurchaseRequestAction;
import apps.promis.out.construction.VwSyncPoContractorHRiseAction;

/**
 * @author chatree
 *
 */
public class ContractContractorHighRiseProcessing extends SyncProcessorCommons {	
	
	private static final Logger log = Logger.getLogger(ContractContractorHighRiseProcessing.class);	
	
	/**
	 * Construction from VO 
	 * if VO type include Purchase Request (PR)
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	@Deprecated
	public void ProcessingContarct(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		log.info("Start ProcessingContarct --->");
		
		try{
			
			VwSyncPoContractorHRiseAction obb = new VwSyncPoContractorHRiseAction();
			System.out.println("company=" + company + " || project=" + project + " || documentNo=" + documentNo + " || status=" + status + " || syncIsstaging=" + syncIsstaging);			
			
			UPD_PROMISUseTime(true, false, null);
			List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);
			
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new VwSyncContractService().SendToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}
			System.out.println("Send MmPocontractorOutb to SAP size=" + CommonService.getSize(value_list));
			
			//find by Vo Docuemnt
			PurchaseRequestAction PurchaseRequestAct = new PurchaseRequestAction();
			List<SyncPrBudget> lstBudget = PurchaseRequestAct.FindByVODocumentNo(company, project, documentNo, status);
			
			if(!CommonService.IsNull(lstBudget)){			
				SyncPrBudget budget = lstBudget.get(0);
				if(budget.getRefType().equals("VO")){
					documentNo = budget.getDocumentNo();
					
					//CreateTo Staging					
					List<MmPurchaseRequest> pr_value_list = new PurchaseRequestAction().CreateToStaging(company, project, documentNo, status, syncIsstaging);
					UPD_PROMISUseTime(false, true, null);
					
					//Send to SAP
					if(CommonService.getSize(pr_value_list) > 0){
						new SyncPrItemService().SendingToSAP(pr_value_list);
						UPD_SENDUseTime(false, true, pr_value_list.get(0).getInstanceNo());
					}					
					System.out.println("Sending Purchase Request To SAP  size="+ CommonService.getSize(pr_value_list) +" --->");
				}
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public void ProcessingContarct1(String company, String project_opt, String documentNo, String status, String syncIsstaging) throws Exception {
		log.info("Start ProcessingContarct --->");
		try{
			int i_interface = 0; 
			
			UPD_PROMISUseTime(true, false, null);
			VwSyncPoContractorHRiseAction obb = new VwSyncPoContractorHRiseAction();
			String project = project_opt;
			String syncIsstaging_opt = "N";
			List<Object[]> lstcont = obb.groupBy(company, project, documentNo, status, syncIsstaging_opt);
			System.out.println("groupBy lstcont size=" + CommonService.getSize(lstcont));
			
			if(CommonService.getSize(lstcont) > 0){
				for(Object[] value : lstcont)
				{
					project = value[1].toString();
					documentNo = value[2].toString();
					
					System.out.println("company="+ company +" || project=" + project + " || documentNo="+documentNo + " || status="+ status + " || syncIsstaging_opt=" +syncIsstaging_opt);
					List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging_opt);
					UPD_PROMISUseTime(false, true, null);
					
					if(CommonService.getSize(value_list) > 0){
						if(i_interface <= 0)
							UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
						
						new VwSyncContractService().SendToSAP(value_list);
						UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
						i_interface++;
					}
					System.out.println("Sending MmPocontractorOutb to SAP size=" + CommonService.getSize(value_list));
					
					//find by Vo Docuemnt
					PurchaseRequestAction PurchaseRequestAct = new PurchaseRequestAction();
					List<SyncPrBudget> lstBudget = PurchaseRequestAct.FindByVODocumentNo(company, project, documentNo, status);
					
					//Don Confirm dont' Tested 2012-07-15 
					if(!CommonService.IsNull(lstBudget)){			
						SyncPrBudget budget = lstBudget.get(0);
						if(budget.getRefType().equals("VO")){
							documentNo = budget.getDocumentNo(); 
							
							//CreateTo Staging
							List<MmPurchaseRequest> pr_value_list = new PurchaseRequestAction().CreateToStaging(company, project, documentNo, status, syncIsstaging_opt);
							UPD_PROMISUseTime(false, true, null);
							
							
							//Send to SAP
							if(CommonService.getSize(pr_value_list) > 0){								
								new SyncPrItemService().SendingToSAP(pr_value_list);
								UPD_SENDUseTime(false, true, null);
							}
							System.out.println("Sending Purchase Request To SAP size=" + CommonService.getSize(pr_value_list) +" --->");
						}
					}
				}
			}else
				UPD_PROMISUseTime(false, true, null);
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	
	public void CancelContract(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		try{
			
			UPD_PROMISUseTime(true, false, null);
			VwSyncPoContractorHRiseAction obb = new VwSyncPoContractorHRiseAction();
			List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);
			
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new VwSyncContractService().SendToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}
			System.out.println("Sended to SAP size="+ CommonService.getSize(value_list));
			
		}catch(Exception e ){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}

	@Deprecated
	private void Start() throws Exception {
		try{
			
			VwSyncPoContractorHRiseAction obb = new VwSyncPoContractorHRiseAction();
			String company = "SP"; 
			String project_opt = null;
			
			/*
			 * 	CT-CH12-000025
				CT-CH12-000028
				CT-CH12-000002
				CT-CH12-000030
				CT-CH12-000031
				CT-CH12-000029
				CT-CH12-000007
				CT-CH12-000015
			 */
			String documentNo = "CW-1207-000004-0";			
			//String documentNo = "VO-1206-000006";
			String status = "A";
			String syncIsstaging_opt = "N";
			List<Object[]> lstcont = obb.groupBy(company, project_opt, documentNo, status, syncIsstaging_opt);
			System.out.println("lstcont size=" + CommonService.getSize(lstcont));
			
			for(Object[] value : lstcont)
			{
				String project = value[1].toString();
				documentNo = value[2].toString();
				
				System.out.println("company="+ company +" || project=" + project + " || documentNo="+documentNo + " || status="+ status + " || syncIsstaging_opt=" +syncIsstaging_opt);
				log.info("company="+ company +" || project=" + project + " || documentNo="+documentNo + " || status="+ status + " || syncIsstaging_opt=" +syncIsstaging_opt);
				
				List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging_opt);
				
				new VwSyncContractService().SendToSAP(value_list);
				log.info("Sending MmPocontractorOutb to SAP size=" + CommonService.getSize(value_list));
				
				//find by Vo Docuemnt
				PurchaseRequestAction PurchaseRequestAct = new PurchaseRequestAction();
				List<SyncPrBudget> lstBudget = PurchaseRequestAct.FindByVODocumentNo(company, project_opt, documentNo, status);
				
				//Don Confirm dont' Tested 2012-07-15 
				if(!CommonService.IsNull(lstBudget)){			
					SyncPrBudget budget = lstBudget.get(0);
					if(budget.getRefType().equals("VO")){
						documentNo = budget.getDocumentNo(); 
						
						//CreateTo Staging
						List<MmPurchaseRequest> pr_value_list = new PurchaseRequestAction().CreateToStaging(company, project_opt, documentNo, status, syncIsstaging_opt);
						
						//Send to SAP
						new SyncPrItemService().SendingToSAP(pr_value_list);
						log.info("Sending Purchase Request To SAP size="+ CommonService.getSize(pr_value_list) +" --->");
					}
				}
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	@Deprecated
	private void ReRunAll() throws Exception {
		try{
			
			String company ="SP";
			String project = "VSK";
			
			List<VwSyncContract> lst = new VwSyncPoContractorHRiseAction().ReRunAll(company, project);
			if(!CommonService.IsNull(lst)){
				
				for(VwSyncContract value : lst)				
					ProcessingContarct(company, project, value.getDocumentNo(), value.getStatus(), value.getSyncIsstaging());
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try{
			
			new ContractContractorHighRiseProcessing().Start();
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
		}
	}

}
